<template>
<div>
  <!--  提示-->
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>您正在做的业务是:</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户修改</el-breadcrumb-item>
    </el-breadcrumb>
  </div>
<!--根据条件查询-->
  <div>
    <!-- 条件查询 -->
    <b>根据姓名查询:</b>
    <el-input v-model="name" style="width: 150px;"/>
    <el-button type="primary" @click="init">查询</el-button>
  </div>

  <!--  表格-->
  <div>
    <el-table
        :data="userData"
        border
        style="width: 100%">
      <el-table-column
          fixed
          prop="loginId"
          label="姓名"
          width="120">
      </el-table-column>
      <el-table-column
          prop="email"
          label="邮箱"
          width="250">
      </el-table-column>
      <el-table-column
          prop="phone"
          label="电话"
          width="180">
      </el-table-column>
      <el-table-column
          prop="address"
          label="地址"
          width="300">
      </el-table-column>
      <el-table-column
          prop="gender"
          label="性别"
          width="120">
        <template slot-scope="scope">
          <p v-if="userData[scope.$index].gender==0">男</p>
          <p v-else-if="userData[scope.$index].gender==1">女</p>
          <p v-else>不男不女</p>
        </template>
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="100">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="medium ">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>

  <!--  分页-->
  <div class="block">
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="Number(pageNum)"
        :page-sizes="[2, 4, 8, 16]"
        :page-size="Number(pageSize)"
        layout="total, sizes, prev, pager, next, jumper"
        :total="Number(total)">
    </el-pagination>
  </div>

<!--  弹出框-->
  <div>
    <el-dialog title="用户修改:" :visible.sync="dialogFormVisible">
      <!--  表单添加-->
      <div>
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="用户名:">
            <el-input v-model="form.loginId"></el-input>
          </el-form-item>
<!--          <el-form-item label="密码:">-->
<!--            <el-input v-model="form.password"></el-input>-->
<!--          </el-form-item>-->
          <el-form-item label="邮箱:">
            <el-input v-model="form.email" disabled="disabled"></el-input>
<!--            <el-button type="primary" @click="sendTheVerificationCode">发送验证码</el-button>-->
          </el-form-item>
          <el-form-item label="性别:">
            <el-radio v-model="form.gender" label="0">男</el-radio>
            <el-radio v-model="form.gender" label="1">女</el-radio>
          </el-form-item>

          <el-form-item label="地址:">
            <el-input v-model="form.address"></el-input>
          </el-form-item>

          <el-form-item label="电话:">
            <el-input v-model="form.phone"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="info" @click="dialogFormVisible=false">取消</el-button>
            <el-button type="primary" @click="update">添加</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</div>
</template>

<script>
import axios from "axios";
import request from "../../request.js"

export default {
  name: "UserUpdate",
  data(){
    return{
      dialogFormVisible:false,
      name: '',
      pageNum: '',
      pageSize: '',
      total: '',
      userData: [],
      form: {
        id:'',
        loginId: '',
        email: '',
        gender: '',
        address: '',
        phone: '',
        createTime: '',
        updateTime: ''
      },
    }
  },methods:{
    //当前数量改变
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.init();
    },
    //页码改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.init();
    },
    init() {
      axios.get("http://localhost:8150/api/ucenter/wx/userQuery", {
        params: {
          'name': this.name,
          'pageNum': this.pageNum,
          'pageSize': this.pageSize
        }
      }).then(data => {
        if (data.data.flag==false) {
          this.$message({
            showClose: true,
            message: data.data.message,
            type: data.data.flag == true ? "success" : "error"
          })
          return;
        }
        this.userData = data.data.data.records;
        this.pageNum = data.data.data.current;
        this.pageSize = data.data.data.size;
        this.total = data.data.data.total;
      }).catch(e => {
        alert("错误信息为:" + e)
      })
    },
    handleClick(row) {
      this.dialogFormVisible=true;
      this.form.loginId=row.loginId;
      this.form.email=row.email;
      this.form.id=row.id;
      this.form.address=row.address;
      this.form.phone=row.phone;
      this.form.gender=row.gender;
    },update(){
      axios.put("http://localhost:8150/api/ucenter/wx/",this.form).then(data =>{


        if (data.data.flag==false) {
          this.$message({
            showClose: true,
            message: data.data.message,
            type: data.data.flag == true ? "success" : "error"
          })
          return;
        }

        if (data.data.flag){
          this.init();
        }
        this.$message({
          showClose:true,
          message:data.data.message,
          type:data.data.flag==true?"success":"error"
        });
        this.dialogFormVisible=false;
      }).catch(e =>{
        alert("错误信息:"+e)
      })
    }
  },created() {
    this.init();
  }
}
</script>

<style scoped>

</style>